/**
 * Created by admin on 2016/12/17.
 */
var url_search1="/web/api/product/productSale/search_productSale";
$(function(){
    //1.初始化Table
    var add_oTable = new add_TableInit();
    add_oTable.Init();
});
var add_TableInit = function () {
    var add_oTableInit = new Object();
    add_oTableInit.Init = function () {
        var url = producturl + url_search1;
        $("#add_tbody_list_table").empty();
        $("#add_product_pc").empty();
        $.ajax({
            url: url,
            dataType:'json',
            type:'post',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            data:{
                "product_id": $.trim($("#add_product_id").val()),
                "product_type": $.trim($("#add_product_type").val()),
                "status": $.trim($("#add_status").val()),
                "product_name": $.trim($("#add_product_name").val()),
                "three_type":$("#add_three_type").val(),
                "NEED_DO_CHECK": "false"
            },
            success: function (data) {
                console.log(data)
                if (data != null && data.result.listProductSale.length>0) {
                    add_setProductTableList(data);
                    var pc = data.result.pageController; //取到pageCount的值(把返回数据转成object类型)
                    var currentPage = pc.currentPage; //得到currentPage
                    var pageCount = pc.totalPages; //得到totalPages
                    var pageSize = pc.pageSize; //得到totalPages
                    var options = {
                        bootstrapMajorVersion: 3, //版本
                        currentPage: currentPage, //当前页数
                        totalPages: pageCount, //总页数
                        itemTexts: function (type, page, current) {
                            switch (type) {
                                case "first":
                                    return "首页";
                                case "prev":
                                    return "上一页";
                                case "next":
                                    return "下一页";
                                case "last":
                                    return "末页";
                                case "page":
                                    return page;
                            }
                        },//点击事件，用于通过Ajax来刷新整个tbody_list_table列表
                        onPageClicked: function (event, originalEvent, type, page) {
                            $.ajax({
                                url: producturl + url_search ,
                                type: "post",
                                xhrFields: {
                                    withCredentials: true
                                },
                                crossDomain: true,
                                data:{
                                    "product_id": $.trim($("#add_product_id").val()),
                                    "product_type": $.trim($("#add_product_type").val()),
                                    "status": $.trim($("#add_status").val()),
                                    "product_name": $.trim($("#add_product_name").val()),
                                    "three_type":$("#add_three_type").val(),
                                    "NEED_DO_CHECK": "false",
                                    "pageNo":page,
                                    "pageSize":pageSize
                                },
                                success: function (data1) {
                                    if (data1 != null) {
                                        add_setProductTableList(data1);
                                    }
                                }
                            });
                        }
                    };
                    $("#add_product_pc").bootstrapPaginator(options);
                }
            }
        });
    }
    return add_oTableInit;
}

function add_setProductTableList(data1){
    if (data1 != null && data1.result.listProductSale.length>0) {
        var tablelistProduct = new Array();
        $.each(data1.result.listProductSale, function (index, item) { //遍历返回的json
            tablelistProduct.push('<tr>');
            var receiveDate = "";
            var receiveTimes = "";
            if(item.three_type == 400 && item.my_attr_json != ""){
                var myAttrJson = eval('(' +item.my_attr_json+ ')');
                console.log(myAttrJson.length)
                if(myAttrJson != null){
                    receiveDate  = myAttrJson[0].attr_value;
                    receiveTimes = myAttrJson[1].attr_value;
                }
            }
            tablelistProduct.push('<td><input type="checkbox" value="'+ item.id + '_' + item.name + '_' + item.price + '_'+ receiveDate + '_'+ receiveTimes+ '_'+ item.product_type + '_' + item.two_type + '_' + item.three_type + '" ></td>');
            tablelistProduct.push('<td>' + (index + 1) + '</td>');
            tablelistProduct.push('<td>' + item.id + " <input type='hidden' id='attr_" + item.id + "' value='" + item.my_attr_json + "' />" + '</td>');
            // switch (item.product_type) {
            //     case 100:
            //         tablelistProduct.push('<td>虚拟</td>');
            //         break;
            //     case 200:
            //         tablelistProduct.push('<td>实物</td>');
            //         break;
            //     default:
            //         tablelistProduct.push('<td>未标识</td>');
            // }
            tablelistProduct.push('<td>' + item.name + '</td>');
            tablelistProduct.push('<td>' + item.price + '</td>');
            tablelistProduct.push('<td>' + receiveDate + '</td>');
            tablelistProduct.push('<td>' + receiveTimes + '</td>');
            tablelistProduct.push('</tr>');
        });
        $("#add_tbody_list_table").html(tablelistProduct.join(''));
    }

}

